package com.itggm.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itggm.DTO.MemberPageDTO;
import com.itggm.mapper.MemberMapper;
import com.itggm.pojo.Member;
import com.itggm.response.ResponseResult;
import com.itggm.service.MemberService;
import com.itggm.utils.IDCardUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月18日 11:50
 * 会员管理
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public ResponseResult page(MemberPageDTO memberPageDTO) {
        PageHelper.startPage(memberPageDTO.getPage(), memberPageDTO.getLimit());
        List<Member> members =memberMapper.page(memberPageDTO.getMemberName(), memberPageDTO.getLevel());
        PageInfo<Member> pageInfo = new PageInfo<>(members);
        return ResponseResult.success().count(pageInfo.getTotal()).data(pageInfo.getList());
    }

    @Override
    public ResponseResult add(Member member) {
       // 1.判定会员名是否存在
        String memberName = member.getMemberName();
        Member member1 = memberMapper.selectMemberByMemberName(memberName);
        if (!ObjectUtils.isEmpty(member1)) {
            return ResponseResult.fail().message("会员名已被占用");
        }

        // 2. 设置默认密码
        String defaultPassword = "111111";
        member.setPassword(SecureUtil.md5(defaultPassword));

        //设置时间字段
        Date createTime = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        member.setCreateTime(createTime);

        // 3. 生成会员卡号
        Member member2 = null;
        String idCard = null;
        do {
            idCard = IDCardUtil.createIDCard();
            member2 = memberMapper.selectMemberByIdCard(idCard);
        } while (!ObjectUtils.isEmpty(member2));

        member.setIdCard(idCard);
        // 4. 添加会员数据  sys_member
        memberMapper.addMember(member);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult editMember(Member member) {
        //设置更新时间
        Date updateTime = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        member.setUpdateTime(updateTime);

        memberMapper.updateMember(member);

        return ResponseResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteMember(List<Integer> ids) {

        memberMapper.deleteMember(ids);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult registerMember(Member member) {

        //  1. 判定会员名是否存在
        Member member1 = memberMapper.selectMemberByMemberName(member.getMemberName());
        if (!ObjectUtils.isEmpty(member1)){
            return  ResponseResult.fail().message("会员名已存在");
        }

        // 2. 密码加密
        String md5Pwd = SecureUtil.md5(member.getPassword());
        member.setPassword(md5Pwd);

        // 3. 生成会员卡号
        Member member2 = null;
        String idCard = null;
        do {
            idCard = IDCardUtil.createIDCard();
            member2 = memberMapper.selectMemberByIdCard(idCard);
        } while (!ObjectUtils.isEmpty(member2));

        member.setIdCard(idCard);

        memberMapper.registerMember(member);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult login(Member member, HttpSession session) {

        String memberName = member.getMemberName();
        Member member1 = memberMapper.selectMemberByMemberName(memberName);
        if (ObjectUtils.isEmpty(member1)){  // true
            return  ResponseResult.fail().message("会员不存在");
        }


        String formPwd = SecureUtil.md5(member.getPassword());
        String dBPassword = member1.getPassword();

        if (!StringUtils.equals(formPwd,dBPassword)){
            return  ResponseResult.fail().message("密码错误");
        }

        session.setAttribute("memberName",member1.getMemberName());
        session.setAttribute("tel",member1.getTel());

        return ResponseResult.success().message("认证通过");
    }
}
